home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Enigma Amiga CD / Listati / 60-Gennaio-esempio1.c next >
C/C++ Source or Header  |  1992-09-02  |  3KB  |  126 lines

  1. /**************************************************************************
  2. * Primo esempio:                                                          *
  3. * crea un task e ci comunica mediante porte e messaggi                    *
  4. **************************************************************************/
  5.  
  6. #include <exec/types.h>
  7. #include <exec/memory.h>
  8. #include <exec/tasks.h>
  9. #include <exec/ports.h>
  10. #include <clib/exec_protos.h>
  11. #include <clib/alib_protos.h>
  12. #include <stdio.h>
  13.  
  14. /* definizione comandi */
  15. #define COMANDO1 0L
  16. #define COMANDO2 1L
  17. #define ESCI     2L
  18.  
  19. /* nome task */
  20. #define NOMETASK "esempio1_task"
  21.  
  22. /* grandezza stack */
  23. #define GRANDSTACK 20000
  24.  
  25. /* struttura del nostro messaggio */
  26. struct Messaggio
  27. {
  28.   struct Message Msg;
  29.   ULONG Comando,Dato;
  30. };
  31.  
  32. /* variabili globali */
  33. struct MsgPort *porta,*porta2;
  34. struct Task *nuovotask;
  35.  
  36. /* il nostro task, apre la porta per la comunicazione, attende i messaggi e
  37.    li stampa a video; quando il messaggio e' ESCI rimuove la porta e attende
  38.    di venire eliminato dal programma */
  39. void task()
  40. {
  41.   register ULONG ex = 0;
  42.   struct Messaggio *mm;
  43.   
  44.   porta = (struct MsgPort *)CreatePort("esempio1_portaric",0);
  45.   if (porta == NULL)
  46.   {
  47.     printf("Non posso creare la porta\n");
  48.     Wait(0L);
  49.   }
  50.  
  51.   while(ex == 0)
  52.   {
  53.     if ((mm = (struct Messaggio *)GetMsg(porta)) == NULL)
  54.     {
  55.       WaitPort(porta);
  56.       continue;
  57.     }
  58.     printf("Comando - %ld; dato - %ld\n",mm->Comando,mm->Dato);
  59.     if (mm->Comando == ESCI) ex = 1;
  60.     ReplyMsg((struct Message *)mm);
  61.   }
  62.   DeletePort(porta);
  63.   Wait(0L);
  64. }
  65.  
  66. /* ecco la procedura per spedire un messaggio cosi' come e' definita sul
  67.    ROM Kernel Manual che e' assicurata contro ogni imprevisto.
  68.    La funzione ritorna TRUE se ha potuto spedire il messaggio FALSE altrimenti */
  69. BOOL SafePutToPort(struct Message *message, STRPTR portname)
  70. {
  71.   struct MsgPort *port;
  72.   
  73.   Forbid();
  74.   port = (struct MsgPort *)FindPort(portname);
  75.   if (port) PutMsg(port,message);
  76.   Permit();
  77.   return(port ? TRUE : FALSE);
  78. }
  79.  
  80. void main()
  81. {
  82.   struct Messaggio Mess;
  83.   
  84.   /* creazione porta per la trasmissione */
  85.   porta2 = (struct MsgPort *)CreatePort("esempio1_portatra",0);
  86.   if (porta2 == NULL)
  87.   {
  88.     printf("Non posso creare la porta\n");
  89.     exit(0);
  90.   }
  91.  
  92.   /* creazione task */
  93.   nuovotask = (struct Task *)CreateTask(NOMETASK,0,task,GRANDSTACK);
  94.   if (task == NULL)
  95.   {
  96.     DeletePort(porta2);
  97.     printf("Non posso creare il task\n");
  98.     exit(0);
  99.   }
  100.  
  101.   /* Attendiamo che il task faccia le dovute inizializzazioni */
  102.   Delay(20);
  103.   /* preparazione messaggio */
  104.   Mess.Msg.mn_Node.ln_Type = NT_MESSAGE;
  105.   Mess.Msg.mn_Length = sizeof(struct Messaggio);
  106.   Mess.Msg.mn_ReplyPort = porta2;
  107.   Mess.Comando = COMANDO1;
  108.   Mess.Dato = 100;
  109.   /* invio messaggio */
  110.   if (SafePutToPort((struct Message *)(&Mess),"esempio1_portaric") == FALSE)
  111.     printf("Messaggio non spedito\n");
  112.  
  113.   Delay(20);
  114.   Mess.Comando = ESCI;
  115.   Mess.Dato = 200;
  116.   if (SafePutToPort((struct Message *)(&Mess),"esempio1_portaric") == FALSE)
  117.     printf("Messaggio non spedito\n");
  118.  
  119.   /* aspettiamo che il task liberi tutte le risorse precedentemente occupate */
  120.   Delay(20);
  121.   Forbid();
  122.   DeletePort(porta2);
  123.   DeleteTask(nuovotask);
  124.   Permit();
  125. }
  126.